package com.kweative.model.column;

import com.kweative.model.Table;
import com.kweative.model.sql.SQLDataType;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.time.DateFormatUtils;

/**
 *
 * @author henrik
 */
public class ColumnDate<T extends Table> extends Column<ColumnDate,T> {

    public ColumnDate(String name) {
        this();
		setName(name);
    }

	public ColumnDate() {
		super(Date.class);
		setSQLDataType(SQLDataType.DATE);
		setSQLNotNull(false);
	}


    @Override
    public String serialize(Object value) {
		if (value == null) return null;
        Date d = (Date) value;
        return "\"" + DateFormatUtils.ISO_DATETIME_FORMAT.format(d) +"\"";
    }

    @Override
    public Date unserialize(String value) {
		if (value == null) return null;
        try {
            return DateFormat.getInstance().parse(value);
        } catch (ParseException ex) {
            Logger.getLogger(ColumnDate.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


}
